package com.snapp.web.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.snapp.util.object.model.ObjectModel;
import com.snapp.web.SNAppAction;
import com.snapp.web.agents.SNAppAgent;

public class SaveList extends SNAppAction {
	private HashMap<String,Object> parameterMap = null;
	private String delimiter = ",";
	
	public String execute(HttpServletRequest req) {
		setSNAppGlobalId(req.getParameter("SNAppGlobalId"));
		if (getSNAppGlobalId() == null) {
			setResultCode(-1);
			setResultMessage("Missing \"SNAppGlobalId\" parameter.  Not sure which object to load.");
			return super.execute(req);
		}
		
		try {
			if (req.getParameter("delimiter") != null) setDelimiter(req.getParameter("delimiter"));
			
			if (parameterMap == null) parameterMap = consolidateWriteMap(req);
			
			SNAppAgent agent = new SNAppAgent();
			ObjectModel dbo = agent.saveList(getSNAppGlobalId(), parameterMap, delimiter);
			
			setResultCode(1);
			setResultMessage("Object list saved successfully.");

			setBodyElement(dbo.getBean().toXML());
	
		} catch(Exception e) {
			log("Error parsing dbo bean to xml.", e);
		}
		return super.execute(req);
	}

	public HashMap<String, Object> getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(HashMap<String, Object> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public void setDelimiter(String delimiter) {
		this.delimiter = delimiter;
	}

	public String getDelimiter() {
		return delimiter;
	}

}
